#include "TimerBar.h"

// Initialize the instance to null
TimerBar* TimerBar::m_TimerBarInstance = 0;

TimerBar::TimerBar(void)
{
}

TimerBar::~TimerBar(void)
{
}

TimerBar* TimerBar::GetInstance(void)
{
	// Create the singleton if it doesn't exist
	if(!m_TimerBarInstance)
		m_TimerBarInstance = new TimerBar;

	// Return the singleton
	return m_TimerBarInstance;
}

void TimerBar::DeleteTimerBarInstance(void)
{
	// If instance exist, delete it!
	if(m_TimerBarInstance)
	{
		delete m_TimerBarInstance;
		m_TimerBarInstance = NULL;
	}
}

void TimerBar::Initialize(void)
{
	// Get the instance and initialize the sprite manager
	m_pSpriteManager = SpriteManager::GetInstance();

	//iTimer = 0; 
	sEnergyBarPiece = 10;
	sCheckpointEnergy = 10;
}

void TimerBar::LoadTimerBar(bool bLevelLoad)
{
	// Load in the sprites
	sEnergyBarFull = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar0.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBar1 = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar1.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBar2 = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar2.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBar3 = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar3.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBar4 = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar4.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBar5 = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar5.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBar6 = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar6.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBar7 = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar7.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBar8 = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar8.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBar9 = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar9.png", D3DXVECTOR3(485, 657, 0), 0.6f);
	sEnergyBarEmpty = m_pSpriteManager->LoadSprite("UISprites\\EnergyBar10.png", D3DXVECTOR3(485, 657, 0), 0.6f);

	iTimer = 0;

	if(bLevelLoad)
	{
		sEnergyBarPiece = sEnergyBarFull;
		sCheckpointEnergy = sEnergyBarFull;
	}
	// Initialize timer variables
	lStartTime = (long)timeGetTime();
}

void TimerBar::Update(int iModelCount, int iPreLoadedModels)
{
	lCurrentTime = (LONG)timeGetTime();
	
	//measuring time in miliseconds
	lCurrentTime = lCurrentTime - lStartTime;

	// timeGetTime gives us miliseconds so every 1000 seconds should be 1 second
	// after 1 second timer goes down by one
	if( lCurrentTime > 1500)
	{
		iTimer++;
		if (iTimer > 1)
		{
			if( sEnergyBarPiece == sEnergyBarFull)
				sEnergyBarPiece = sEnergyBar1;
			else if ( sEnergyBarPiece == sEnergyBar1 )
				sEnergyBarPiece = sEnergyBar2;
			else if ( sEnergyBarPiece == sEnergyBar2 )
				sEnergyBarPiece = sEnergyBar3;
			else if ( sEnergyBarPiece == sEnergyBar3 )
				sEnergyBarPiece = sEnergyBar4;
			else if ( sEnergyBarPiece == sEnergyBar4 )
				sEnergyBarPiece = sEnergyBar5;
			else if ( sEnergyBarPiece == sEnergyBar5 )
				sEnergyBarPiece = sEnergyBar6;
			else if ( sEnergyBarPiece == sEnergyBar6 )
				sEnergyBarPiece = sEnergyBar7;
			else if ( sEnergyBarPiece == sEnergyBar7 )
				sEnergyBarPiece = sEnergyBar8;
			else if ( sEnergyBarPiece == sEnergyBar8 )
				sEnergyBarPiece = sEnergyBar9;
			else if ( sEnergyBarPiece == sEnergyBar9 )
				sEnergyBarPiece = sEnergyBarEmpty;
			iTimer = 0;
		}
		lStartTime = (long)timeGetTime();
	}
}

void TimerBar::DamageTaken(void)
{
	if(sEnergyBarPiece + 2 <= sEnergyBarEmpty)
		sEnergyBarPiece += 2;
	else
		sEnergyBarPiece = sEnergyBarEmpty;
}

int TimerBar::CheckTime()
{
	// Check how much time is left
	return (int)sEnergyBarPiece;
}

void TimerBar::SetCheckpoint(void)
{
	// Set the checkpoint amount
	sCheckpointEnergy = sEnergyBarPiece;
}

void TimerBar::ResetTime()
{
	// Set the energy piece to the checkpoint
	sEnergyBarPiece = sCheckpointEnergy;
}

void TimerBar::FullEnergy()
{
	// Full energy is gained
	sEnergyBarPiece = sEnergyBarFull;
}

void TimerBar::Render(void)
{
	// Draw
	m_pSpriteManager->DrawSprite(sEnergyBarPiece);
}

void TimerBar::DestroyTimer(void)
{
	for(short i = sEnergyBarEmpty; i >= sEnergyBarFull; i--)
		m_pSpriteManager->DestroySprite(i);
}